package com.advertisementsystem.Servie.Impl;

import com.advertisementsystem.Mapper.SysLoginLogMapper;
import com.advertisementsystem.Mapper.SysOperationLogMapper;
import com.advertisementsystem.Servie.SysLogService;
import com.advertisementsystem.entity.SysLoginLog;
import com.advertisementsystem.entity.SysOperationLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SysLogServiceImpl implements SysLogService {

    @Autowired
    private SysOperationLogMapper operationLogMapper;

    @Autowired
    private SysLoginLogMapper loginLogMapper;

    @Override
    public Page<SysOperationLog> getOperationLogList(SysOperationLog operLog, Page<SysOperationLog> page) {
        LambdaQueryWrapper<SysOperationLog> lqw = new LambdaQueryWrapper<SysOperationLog>()
                .like(StringUtils.hasText(operLog.getTitle()), SysOperationLog::getTitle, operLog.getTitle())
                .eq(operLog.getBusinessType() != null, SysOperationLog::getBusinessType, operLog.getBusinessType())
                .like(StringUtils.hasText(operLog.getOperatorName()), SysOperationLog::getOperatorName, operLog.getOperatorName())
                .eq(operLog.getStatus() != null, SysOperationLog::getStatus, operLog.getStatus())
                .orderByDesc(SysOperationLog::getOperatorTime);

        return operationLogMapper.selectPage(page, lqw);
    }

    @Override
    public Page<SysLoginLog> getLoginLogList(SysLoginLog loginLog, Page<SysLoginLog> page) {
        LambdaQueryWrapper<SysLoginLog> lqw = new LambdaQueryWrapper<SysLoginLog>()
                .like(StringUtils.hasText(loginLog.getUserName()), SysLoginLog::getUserName, loginLog.getUserName())
                .eq(StringUtils.hasText(loginLog.getIpAddr()), SysLoginLog::getIpAddr, loginLog.getIpAddr())
                .eq(loginLog.getStatus() != null, SysLoginLog::getStatus, loginLog.getStatus())
                .orderByDesc(SysLoginLog::getLoginTime);

        return loginLogMapper.selectPage(page, lqw);
    }

    @Override
    public void deleteOperationLogByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        // 使用MyBatis-Plus的批量删除
        operationLogMapper.deleteBatchIds(idList);
    }

    @Override
    public void deleteLoginLogByIds(String ids) {
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::valueOf)
                .collect(Collectors.toList());
        // 使用MyBatis-Plus的批量删除
        loginLogMapper.deleteBatchIds(idList);
    }



    @Override
    public void cleanOperationLog() {
        // 使用MyBatis-Plus的删除所有
        LambdaQueryWrapper<SysOperationLog> wrapper = new LambdaQueryWrapper<>();
        operationLogMapper.delete(wrapper);
    }

    @Override
    public void cleanLoginLog() {
        // 使用MyBatis-Plus的删除所有
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();
        loginLogMapper.delete(wrapper);
    }

    @Override
    public void recordOperationLog(SysOperationLog operLog) {
        operLog.setOperatorTime(LocalDateTime.now());
        operationLogMapper.insert(operLog);
    }

    @Override
    public void recordLoginLog(SysLoginLog loginLog) {
        loginLog.setLoginTime(LocalDateTime.now());
        loginLogMapper.insert(loginLog);
    }
}